package suanfa.datastructure.queue;

public class Linkqueue<T> {
    node front,rear;
    int size;
    class node{
        public T data;
        public node next;

        public node(T data){
            this.data=data;
        }

    }
    public Linkqueue(){
        this.front=null;
        this.rear=null;
        size=0;
    }

    public boolean isEmpty(){
        return this.front==null&&rear==null;
    }
    public int getsize(){
        return size;
    }
    public void enqueue(T data){
        node n=new node(data);

        if(isEmpty()){
            this.front=n;
        }else{
            this.rear.next=n;
        }
        System.out.println("入队->"+data);
        this.rear=n;
        size++;
    }

    public T dequeue(){
        if(isEmpty()){
            System.out.println("队列为空");
            return null;
        }
        T d=this.front.data;
        this.front=this.front.next;
        if(this.front==null){
            this.rear=null;
        }
        System.out.println("出队->"+d);
        size--;
        return d;

    }

    public T peek(){
        return isEmpty()? null:this.front.data;
    }

    public static void main(String[] args) {
        Linkqueue<String> c=new Linkqueue<>();
        c.enqueue("A");
        c.enqueue("B");
        c.enqueue("C");

        System.out.println("size:"+c.getsize());
        int s=c.getsize();
        for(int i=0;i<s;i++){
            c.dequeue();
        }
        c.enqueue("D");
    }
}
